package User;

import Book.Book;
import Book.PairOfUidAndBookId;
import Book.Library;
import Utils.AnalyzingBorrowBook;
import Utils.PermissionException;
import Utils.ScannerSingleton;
import constant.constant;
import java.util.Scanner;

import static constant.constant.MAX_BORROW_BOOK;

public class NormalUser extends  User {
    private PairOfUidAndBookId[] pairOfUidAndBookIds;
    private int borrowcount;
    private final AnalyzingBorrowBook analyzingBook = new AnalyzingBorrowBook();
    private constant constant = new constant();
    public Library library;
    //    public Scanner scanner = new Scanner(System.in);
    public Scanner scanner;

    public NormalUser(String name, int userid) {
        super(name, userid, "普通用户");
    }

    @Override
    public int display() {
        scanner = ScannerSingleton.getScanner();
        System.out.println(" 普通⽤⼾ " + this.name + " 的操作菜单:");
        System.out.println("1. 查找图书 ");
        System.out.println("2. 打印所有的图书 ");
        System.out.println("3. 退出系统 ");
        System.out.println("4. 借阅图书 ");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个⼈借阅情况");
        System.out.println(" 请选择你的操作： ");
        return scanner.nextInt();
    }

    private void loadborrowbook() {
        PairOfUidAndBookId[] allborrowbook;
        try {
            allborrowbook = analyzingBook.loadObject(constant.BORROWED_BOOK_FILE_NAME);
            pairOfUidAndBookIds = new PairOfUidAndBookId[MAX_BORROW_BOOK];
            if (allborrowbook == null) {
                borrowcount = 0;
            } else {
                int allbookborrowlen = allborrowbook.length;
                if (allbookborrowlen > pairOfUidAndBookIds.length) {
                    pairOfUidAndBookIds = new PairOfUidAndBookId[allbookborrowlen];
                }
                for (int i = 0; i < allbookborrowlen; i++) {
                    pairOfUidAndBookIds[i] = allborrowbook[i];
                }
                borrowcount = allbookborrowlen;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void storeborrowbook() {
        try {
            analyzingBook.storeObject(pairOfUidAndBookIds, constant.BORROWED_BOOK_FILE_NAME);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //借阅图书
    public void borrowBook() {
        scanner.nextLine();
        System.out.println("请输入要借阅的图书编号：");
        int bookid = scanner.nextInt();
        scanner.nextLine();
        library = Library.getLibrary();
        if (library.bookCount == 0) {
            System.out.println("没有可借图书！");
            return;
        }
        loadborrowbook();
        Book book = library.searchBook(bookid);
        if (book == null) {
            System.out.println("没有该图书！");
            return;
        }
        for (int i = 0; i < borrowcount; i++) {
            PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
            if (pairOfUidAndBookId.getBookid() == bookid) {
                System.out.println("该图书你已借阅！");
                return;
            } else {
                System.out.println("该图书其他人已借阅！");
                return;
            }
        }

        library.borrowbook(bookid);
        PairOfUidAndBookId pairOfUidAndBookId = new PairOfUidAndBookId(getUserid(), bookid);
        pairOfUidAndBookIds[borrowcount] = pairOfUidAndBookId;
        borrowcount++;
        storeborrowbook();
        System.out.println("借阅成功！");
    }

    //归还图书
    public void returnBook() {
        loadborrowbook();
        if (borrowcount == 0) {
            System.out.println("没有可还图书！");
            return;
        }
        scanner.nextLine();
        int bookid = scanner.nextInt();
        System.out.println("请输入要归还的图书编号：");
        Book book = library.searchBook(bookid);
        if (book == null) {
            System.out.println("没有该图书！");
            return;
        }
        for (int i = 0; i < borrowcount; i++) {
            if (pairOfUidAndBookIds[i].getBookid() == bookid) {
                if (getUserid() == pairOfUidAndBookIds[i].getUserid()) {
                    library.returnbook(bookid);
                    System.out.println("归还成功！");
                    pairOfUidAndBookIds[i] = pairOfUidAndBookIds[borrowcount - 1];
                    pairOfUidAndBookIds[borrowcount - 1] = null;
                    borrowcount--;
                    storeborrowbook();
                } else {
                    System.out.println("你没有借阅该图书！");
                }
                return;
            }
        }


        //查看个⼈借阅情况
//            public void viewBorrowBooks(){
//            }
    }

    public void viewBorrowBooks() {
        if (borrowcount == 0){
            System.out.println("没有可还图书！");
        }else {
            boolean flag = false;
            for (int i = 0; i < borrowcount; i++) {
                if (pairOfUidAndBookIds[i].getUserid() == getUserid()) {
                    flag = true;
                    Book book = library.searchBook(pairOfUidAndBookIds[i].getBookid());
                    System.out.println(book);
                }
            }
            if (!flag) {
                System.out.println("没有可还图书！");
            }
        }
    }
}
